<!DOCTYPE HTML>
<html lang="zh-tw" >
    <!-- Start book django项目 -->
    <head>
        <!-- head:start -->
        <meta charset="UTF-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <title>响应Response | django项目</title>
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <meta name="description" content="">
        <meta name="generator" content="GitBook 2.6.7">
        
        
        <meta name="HandheldFriendly" content="true"/>
        <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
        <meta name="apple-mobile-web-app-capable" content="yes">
        <meta name="apple-mobile-web-app-status-bar-style" content="black">
        <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
        <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">
        
    <link rel="stylesheet" href="../gitbook/style.css">
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-splitter/splitter.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-emphasize/plugin.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-fontsettings/website.css">
        
    
        
        <link rel="stylesheet" href="../gitbook/plugins/gitbook-plugin-highlight/website.css">
        
    
    

        
    
    
    <link rel="next" href="../C03-RequestAndResponse/Cookie.html" />
    
    
    <link rel="prev" href="../C03-RequestAndResponse/Request.html" />
    

        <!-- head:end -->
    </head>
    <body>
        <!-- body:start -->
        
    <div class="book"
        data-level="4.2"
        data-chapter-title="响应Response"
        data-filepath="C03-RequestAndResponse/Response.md"
        data-basepath=".."
        data-revision="Wed Nov 14 2018 07:27:32 GMT+0800 (CST)"
        data-innerlanguage="">
    

<div class="book-summary">
    <nav role="navigation">
        <ul class="summary">
            
            
            
            

            

            
    
        <li class="chapter " data-level="0" data-path="index.html">
            
                
                    <a href="../index.html">
                
                        <i class="fa fa-check"></i>
                        
                        Python Web 框架要点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="1" data-path="AboutDjango.html">
            
                
                    <a href="../AboutDjango.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>1.</b>
                        
                        Django介绍
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2" data-path="C01-CreateProject/index.html">
            
                
                    <a href="../C01-CreateProject/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.</b>
                        
                        工程搭建
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="2.1" data-path="C01-CreateProject/EnvInstall.html">
            
                
                    <a href="../C01-CreateProject/EnvInstall.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.1.</b>
                        
                        环境安装
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.2" data-path="C01-CreateProject/StartProject.html">
            
                
                    <a href="../C01-CreateProject/StartProject.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.2.</b>
                        
                        创建工程
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.3" data-path="C01-CreateProject/StartApp.html">
            
                
                    <a href="../C01-CreateProject/StartApp.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.3.</b>
                        
                        创建子应用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="2.4" data-path="C01-CreateProject/CreateView.html">
            
                
                    <a href="../C01-CreateProject/CreateView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>2.4.</b>
                        
                        创建视图
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="3" data-path="C02-SettingsStaticAndURL/index.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.</b>
                        
                        配置、静态文件与路由
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="3.1" data-path="C02-SettingsStaticAndURL/Settings.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/Settings.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.1.</b>
                        
                        配置文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.2" data-path="C02-SettingsStaticAndURL/StaticFiles.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/StaticFiles.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.2.</b>
                        
                        静态文件
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="3.3" data-path="C02-SettingsStaticAndURL/URL.html">
            
                
                    <a href="../C02-SettingsStaticAndURL/URL.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>3.3.</b>
                        
                        路由说明
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="4" data-path="C03-RequestAndResponse/index.html">
            
                
                    <a href="../C03-RequestAndResponse/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.</b>
                        
                        请求与响应
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="4.1" data-path="C03-RequestAndResponse/Request.html">
            
                
                    <a href="../C03-RequestAndResponse/Request.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.1.</b>
                        
                        请求Request
                    </a>
            
            
        </li>
    
        <li class="chapter active" data-level="4.2" data-path="C03-RequestAndResponse/Response.html">
            
                
                    <a href="../C03-RequestAndResponse/Response.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.2.</b>
                        
                        响应Response
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.3" data-path="C03-RequestAndResponse/Cookie.html">
            
                
                    <a href="../C03-RequestAndResponse/Cookie.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.3.</b>
                        
                        Cookie
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="4.4" data-path="C03-RequestAndResponse/Session.html">
            
                
                    <a href="../C03-RequestAndResponse/Session.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>4.4.</b>
                        
                        Session
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="5" data-path="C04-ClassViewAndMiddleware/index.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.</b>
                        
                        类视图与中间件
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="5.1" data-path="C04-ClassViewAndMiddleware/ClassView.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/ClassView.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.1.</b>
                        
                        类视图
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="5.2" data-path="C04-ClassViewAndMiddleware/Middleware.html">
            
                
                    <a href="../C04-ClassViewAndMiddleware/Middleware.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>5.2.</b>
                        
                        中间件
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="6" data-path="C05-Template/index.html">
            
                
                    <a href="../C05-Template/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.</b>
                        
                        模板
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="6.1" data-path="C05-Template/template.html">
            
                
                    <a href="../C05-Template/template.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>6.1.</b>
                        
                        模板使用
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="7" data-path="C06-Database/index.html">
            
                
                    <a href="../C06-Database/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.</b>
                        
                        数据库
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="7.1" data-path="C06-Database/Config.html">
            
                
                    <a href="../C06-Database/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.1.</b>
                        
                        配置
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.2" data-path="C06-Database/Model.html">
            
                
                    <a href="../C06-Database/Model.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.2.</b>
                        
                        定义模型类
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.3" data-path="C06-Database/Tools.html">
            
                
                    <a href="../C06-Database/Tools.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.3.</b>
                        
                        演示工具使用
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.4" data-path="C06-Database/CURD.html">
            
                
                    <a href="../C06-Database/CURD.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.4.</b>
                        
                        数据库操作
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="7.5" data-path="C06-Database/QuerySet.html">
            
                
                    <a href="../C06-Database/QuerySet.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>7.5.</b>
                        
                        查询集QuerySet
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="8" data-path="C07-Admin/index.html">
            
                
                    <a href="../C07-Admin/index.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.</b>
                        
                        Admin站点
                    </a>
            
            
            <ul class="articles">
                
    
        <li class="chapter " data-level="8.1" data-path="C07-Admin/Config.html">
            
                
                    <a href="../C07-Admin/Config.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.1.</b>
                        
                        使用Admin站点
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.2" data-path="C07-Admin/ModifyListDisplay.html">
            
                
                    <a href="../C07-Admin/ModifyListDisplay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.2.</b>
                        
                        调整列表页展示
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.3" data-path="C07-Admin/ModifyEditDisplay.html">
            
                
                    <a href="../C07-Admin/ModifyEditDisplay.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.3.</b>
                        
                        调整编辑页展示
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.4" data-path="C07-Admin/ModifySite.html">
            
                
                    <a href="../C07-Admin/ModifySite.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.4.</b>
                        
                        调整站点信息
                    </a>
            
            
        </li>
    
        <li class="chapter " data-level="8.5" data-path="C07-Admin/UploadImage.html">
            
                
                    <a href="../C07-Admin/UploadImage.html">
                
                        <i class="fa fa-check"></i>
                        
                            <b>8.5.</b>
                        
                        上传图片
                    </a>
            
            
        </li>
    

            </ul>
            
        </li>
    


            
            <li class="divider"></li>
            <li>
                <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
                    本書使用 GitBook 釋出
                </a>
            </li>
            
        </ul>
    </nav>
</div>

    <div class="book-body">
        <div class="body-inner">
            <div class="book-header" role="navigation">
    <!-- Actions Left -->
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href="../" >django项目</a>
    </h1>
</div>

            <div class="page-wrapper" tabindex="-1" role="main">
                <div class="page-inner">
                
                
                    <section class="normal" id="section-">
                    
                        <h1 id="&#x54CD;&#x5E94;">&#x54CD;&#x5E94;</h1>
<p>&#x89C6;&#x56FE;&#x5728;&#x63A5;&#x6536;&#x8BF7;&#x6C42;&#x5E76;&#x5904;&#x7406;&#x540E;&#xFF0C;&#x5FC5;&#x987B;&#x8FD4;&#x56DE;HttpResponse&#x5BF9;&#x8C61;&#x6216;&#x5B50;&#x5BF9;&#x8C61;&#x3002;HttpRequest&#x5BF9;&#x8C61;&#x7531;Django&#x521B;&#x5EFA;&#xFF0C;HttpResponse&#x5BF9;&#x8C61;&#x7531;&#x5F00;&#x53D1;&#x4EBA;&#x5458;&#x521B;&#x5EFA;&#x3002;</p>
<h2 id="1-httpresponse">1 HttpResponse</h2>
<p>&#x53EF;&#x4EE5;&#x4F7F;&#x7528;<strong>django.http.HttpResponse</strong>&#x6765;&#x6784;&#x9020;&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#x3002;</p>
<pre><code class="lang-python">HttpResponse(content=&#x54CD;&#x5E94;&#x4F53;, content_type=&#x54CD;&#x5E94;&#x4F53;&#x6570;&#x636E;&#x7C7B;&#x578B;, status=&#x72B6;&#x6001;&#x7801;)
</code></pre>
<p>&#x4E5F;&#x53EF;&#x901A;&#x8FC7;HttpResponse&#x5BF9;&#x8C61;&#x5C5E;&#x6027;&#x6765;&#x8BBE;&#x7F6E;&#x54CD;&#x5E94;&#x4F53;&#x3001;&#x72B6;&#x6001;&#x7801;&#xFF1A;</p>
<ul>
<li>content&#xFF1A;&#x8868;&#x793A;&#x8FD4;&#x56DE;&#x7684;&#x5185;&#x5BB9;&#x3002;</li>
<li>status_code&#xFF1A;&#x8FD4;&#x56DE;&#x7684;HTTP&#x54CD;&#x5E94;&#x72B6;&#x6001;&#x7801;&#x3002;</li>
</ul>
<p>&#x54CD;&#x5E94;&#x5934;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x5C06;HttpResponse&#x5BF9;&#x8C61;&#x5F53;&#x505A;&#x5B57;&#x5178;&#x8FDB;&#x884C;&#x54CD;&#x5E94;&#x5934;&#x952E;&#x503C;&#x5BF9;&#x7684;&#x8BBE;&#x7F6E;&#xFF1A;</p>
<pre><code class="lang-python">response = HttpResponse()
response[<span class="hljs-string">&apos;Itcast&apos;</span>] = <span class="hljs-string">&apos;Python&apos;</span>  <span class="hljs-comment"># &#x81EA;&#x5B9A;&#x4E49;&#x54CD;&#x5E94;&#x5934;Itcast, &#x503C;&#x4E3A;Python</span>
</code></pre>
<p>&#x793A;&#x4F8B;&#xFF1A;</p>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.http <span class="hljs-keyword">import</span> HttpResponse

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">demo_view</span><span class="hljs-params">(request)</span>:</span>
    <span class="hljs-keyword">return</span> HttpResponse(<span class="hljs-string">&apos;itcast python&apos;</span>, status=<span class="hljs-number">400</span>)
    &#x6216;&#x8005;
    response = HttpResponse(<span class="hljs-string">&apos;itcast python&apos;</span>)
    response.status_code = <span class="hljs-number">400</span>
    response[<span class="hljs-string">&apos;Itcast&apos;</span>] = <span class="hljs-string">&apos;Python&apos;</span>
    <span class="hljs-keyword">return</span> response
</code></pre>
<h2 id="2-httpresponse&#x5B50;&#x7C7B;">2 HttpResponse&#x5B50;&#x7C7B;</h2>
<p>Django&#x63D0;&#x4F9B;&#x4E86;&#x4E00;&#x7CFB;&#x5217;HttpResponse&#x7684;&#x5B50;&#x7C7B;&#xFF0C;&#x53EF;&#x4EE5;&#x5FEB;&#x901F;&#x8BBE;&#x7F6E;&#x72B6;&#x6001;&#x7801;</p>
<ul>
<li>HttpResponseRedirect  301</li>
<li>HttpResponsePermanentRedirect  302</li>
<li>HttpResponseNotModified  304</li>
<li>HttpResponseBadRequest  400</li>
<li>HttpResponseNotFound  404</li>
<li>HttpResponseForbidden  403</li>
<li>HttpResponseNotAllowed  405</li>
<li>HttpResponseGone  410</li>
<li>HttpResponseServerError  500</li>
</ul>
<h2 id="3-jsonresponse">3 JsonResponse</h2>
<p>&#x82E5;&#x8981;&#x8FD4;&#x56DE;json&#x6570;&#x636E;&#xFF0C;&#x53EF;&#x4EE5;&#x4F7F;&#x7528;JsonResponse&#x6765;&#x6784;&#x9020;&#x54CD;&#x5E94;&#x5BF9;&#x8C61;&#xFF0C;&#x4F5C;&#x7528;&#xFF1A;</p>
<ul>
<li>&#x5E2E;&#x52A9;&#x6211;&#x4EEC;&#x5C06;&#x6570;&#x636E;&#x8F6C;&#x6362;&#x4E3A;json&#x5B57;&#x7B26;&#x4E32;</li>
<li>&#x8BBE;&#x7F6E;&#x54CD;&#x5E94;&#x5934;<strong>Content-Type</strong>&#x4E3A; <strong>application/json</strong></li>
</ul>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.http <span class="hljs-keyword">import</span> JsonResponse

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">demo_view</span><span class="hljs-params">(request)</span>:</span>
    <span class="hljs-keyword">return</span> JsonResponse({<span class="hljs-string">&apos;city&apos;</span>: <span class="hljs-string">&apos;beijing&apos;</span>, <span class="hljs-string">&apos;subject&apos;</span>: <span class="hljs-string">&apos;python&apos;</span>})
</code></pre>
<h2 id="4-redirect&#x91CD;&#x5B9A;&#x5411;">4 redirect&#x91CD;&#x5B9A;&#x5411;</h2>
<pre><code class="lang-python"><span class="hljs-keyword">from</span> django.shortcuts <span class="hljs-keyword">import</span> redirect

<span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">demo_view</span><span class="hljs-params">(request)</span>:</span>
    <span class="hljs-keyword">return</span> redirect(<span class="hljs-string">&apos;/index.html&apos;</span>)
</code></pre>

                    
                    </section>
                
                
                </div>
            </div>
        </div>

        
        <a href="../C03-RequestAndResponse/Request.html" class="navigation navigation-prev " aria-label="Previous page: 请求Request"><i class="fa fa-angle-left"></i></a>
        
        
        <a href="../C03-RequestAndResponse/Cookie.html" class="navigation navigation-next " aria-label="Next page: Cookie"><i class="fa fa-angle-right"></i></a>
        
    </div>
</div>

        
<script src="../gitbook/app.js"></script>

    
    <script src="../gitbook/plugins/gitbook-plugin-toggle-chapters/toggle.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-splitter/splitter.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-fontsettings/buttons.js"></script>
    

    
    <script src="../gitbook/plugins/gitbook-plugin-sharing/buttons.js"></script>
    

<script>
require(["gitbook"], function(gitbook) {
    var config = {"fontsettings":{"family":"sans","size":2,"theme":"white"},"toggle-chapters":{},"splitter":{},"emphasize":{},"highlight":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]}};
    gitbook.start(config);
});
</script>

        <!-- body:end -->
    </body>
    <!-- End of book django项目 -->
</html>
